﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FiFaChamps.Datahandle;
using System.Data;
using FiFaChamps.Engine.Repository;

namespace FiFaChamps.Repository 
{
    public class PlayerRepo : BaseRepo
    {
        fifaChampsDataDataContext db = DataManager.FifaDB();
        
       public string deletePlayer(Player player) 
       {
           var deletePlayer =
               from pl in db.FCT_Players
               where pl.ID == player.id
               select pl;

           foreach(var p in deletePlayer)
           {
                db.FCT_Players.DeleteOnSubmit(p);
           }

           return Commiter(db,"Player Delete");
           
       }
 
       public string insertPlayer(Player player)
       {
           FCT_Player p = new FCT_Player()
           {
               Name = player.name,
               Surname = player.surname,
               Email = player.email,
               Phone = player.phone,
               Nickname = player.nickname,
               OnlinePlayer = player.onlineplayer,
               FirstTeam = player.firstTeam,
               SecondTeam = player.secondTeam,
               PlayerPicture = player.playerPicture
           };

           db.FCT_Players.InsertOnSubmit(p);
           
           return Commiter(db, "Player Created.");
       }

       public string updatePlayer(Player player)
       {
           var upatePlayer =
             from pl in db.FCT_Players
             where pl.ID == player.id
             select pl;

           foreach (var p in upatePlayer)
           {
               p.Name = player.name;
               p.Surname = player.surname;
               p.Email = player.email;
               p.Phone = player.phone;
               p.Nickname = player.nickname;
               p.OnlinePlayer = player.onlineplayer;
               p.FirstTeam = player.firstTeam;
               p.SecondTeam = player.secondTeam;
               p.PlayerPicture = player.playerPicture;
           }

            return Commiter(db, "Player Updated.");
       }

       public List<Player> getAllPlayers()
       {
           List<Player> playerList = new List<Player>();
           Player player = new Player();
       
               var q =
                        from p in db.FCT_Players
                        where p.ID != null
                        select p;

                        foreach( var p in q)
                        {
                            player.name = p.Name  ;
                            player.surname = p.Surname  ;
                            player.email = p.Email  ;
                            player.phone = p.Phone  ;
                            player.nickname = p.Nickname  ;
                            player.onlineplayer = p.OnlinePlayer ;
                            player.firstTeam = p.FirstTeam ;
                            player.secondTeam = p.SecondTeam  ;
                            player.playerPicture = p.PlayerPicture ;
                            playerList.Add(player);
                        };
           

           return playerList;
       }

    }
}
